﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using PoolGame2.Networking;
using PoolGame2.Player;
using PoolGame2.Managers;

namespace PoolGame2.Menus
{
    public class NixonOverlay : Overlay, IGameEventListener
    {
        enum NixonTransitionState
        {
            UP = 0,
            DOWN,
            TALKING,
            HIDDEN,
        };

        public const float HoldTime = 2.5f;

        Texture2D nixonClosed;
        Texture2D nixonOpen;
        Texture2D nixonHappy;
        Texture2D speechBubble;
        Random rand = new Random();
        Vector2 movementPosition = Vector2.Zero;
        Vector2 speechPosition = new Vector2(10, 10);
        NixonTransitionState TransitionState = NixonTransitionState.UP;
        float flTalkTime = 0.0f;

        String sayText;

        Texture2D drawTex;

        float nextFlip = 0.0f;

        public NixonOverlay(PoolGame game)
            : base(game)
        {
            BGAlpha = 0.0f;

            nixonClosed = game.textureDatabase.GetTexture("announcer/Nixon");
            nixonOpen = game.textureDatabase.GetTexture("announcer/NixonChat");
            nixonHappy = game.textureDatabase.GetTexture("announcer/NixonHappy");
            speechBubble = game.textureDatabase.GetTexture("announcer/speech");
            drawTex = nixonOpen;

            sayText = "";

            size = new Vector2(nixonClosed.Width * 2, nixonClosed.Height);
            position = new Vector2(game.GraphicsDevice.Viewport.Width - size.X, game.GraphicsDevice.Viewport.Height - size.Y);
            
            TransitionState = NixonTransitionState.HIDDEN;

            game.eventManager.eventListeners.Add(this);
        }

        public override void Update(GameTime gameTime)
        {
            if (TransitionState == NixonTransitionState.UP)
            {
                movementPosition.Y -= (float)(gameTime.ElapsedGameTime.TotalMilliseconds / 2);
                if (movementPosition.Y < 0.0f)
                {
                    flTalkTime = HoldTime;
                    TransitionState = NixonTransitionState.TALKING;
                }
            }
            else if (TransitionState == NixonTransitionState.DOWN)
            {
                movementPosition.Y += (float)(gameTime.ElapsedGameTime.TotalMilliseconds / 2);
                if (movementPosition.Y > nixonOpen.Height)
                    TransitionState = NixonTransitionState.HIDDEN;
            }
            else if (TransitionState == NixonTransitionState.TALKING)
            {
                flTalkTime -= (float)(gameTime.ElapsedGameTime.TotalMilliseconds / 1000);
                if (flTalkTime < 0.0f)
                {
                    drawTex = nixonClosed;
                    TransitionState = NixonTransitionState.DOWN;
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (TransitionState == NixonTransitionState.HIDDEN)
                return;

            base.Draw(gameTime);

            if (TransitionState == NixonTransitionState.TALKING)
            {
                nextFlip -= (float)(gameTime.ElapsedGameTime.TotalMilliseconds / 1000);
                if (nextFlip < 0.0f)
                {
                    nextFlip = 0.25f;
                    if (drawTex == nixonOpen)
                        drawTex = nixonClosed;
                    else
                        drawTex = nixonOpen;
                }
            }

            PoolGame pGame = (PoolGame)Game;

            SpriteBatch spriteBatch = pGame.spriteBatch;
            SpriteFont font = pGame.fontDatabase.loadFont("Courier New");
            Rectangle fullscreen = new Rectangle((int)(position.X + nixonOpen.Width), (int)(position.Y + movementPosition.Y), (int)nixonOpen.Width, (int)nixonOpen.Height);

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
                spriteBatch.Draw(drawTex, fullscreen, Color.White);
                if (TransitionState == NixonTransitionState.TALKING)
                {
                    spriteBatch.Draw(speechBubble, position, Color.White);
                    spriteBatch.DrawString(font, sayText, position + speechPosition, Color.Black);
                }
            spriteBatch.End();
        }

        public void GameEventPerformed(GameEvent e)
        {
            if (e.EventID != (int)GameEventID.NIXON_SAY)
                return;

            if (e.data == null)
                sayText = e.EventDesc;
            else
                sayText = e.data.ReadString();

            nextFlip = 0.25f;
            drawTex = nixonClosed;
            TransitionState = NixonTransitionState.UP;
            movementPosition.Y = nixonOpen.Height;
        }
    }
}
